package com.xrit.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xrit.sunnyday.dao.MemberDao;
import com.xrit.sunnyday.dao.OrderDao;
import com.xrit.sunnyday.dao.OrderSettingDao;
import com.xrit.sunnyday.exception.SunnydayException;
import com.xrit.sunnyday.pojo.Member;
import com.xrit.sunnyday.pojo.Order;
import com.xrit.sunnyday.pojo.OrderSetting;
import com.xrit.sunnyday.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService{

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    @Transactional
    public Order ordersubmit(Map<String, String> map) throws SunnydayException {
        //获取前端体检日期
        String orderdatestr = map.get("orderDate");
        //转换时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date orderDate=null;
        try {
            orderDate=sdf.parse(orderdatestr);
        } catch (ParseException e) {
            //转换不成功，抛异常
            throw new SunnydayException("日期格式转换失败");
        }
        //查看当前预约日期是否存在
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        //判断日期能不能预约
        if (null==orderSetting){
            //不为空 不能预约
            throw new SunnydayException("当前日期无法预约");
        }
        //判断已预约人数是否大于可预约人数
        if (orderSetting.getReservations()>=orderSetting.getNumber()){
            throw new SunnydayException("当前日期已预约人数已满");
        }
        //获取手机号码
        String telephone=map.get("telephone");
        //查询是否为会员
        Member member = memberDao.findByTelephone(telephone);
        if (null==member){
            //不是 就添加
            member = new Member();
            member.setIdCard(map.get("idCard"));
            member.setName(map.get("name"));
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            member.setSex(map.get("sex"));

            //添加
            memberDao.add(member);
        }
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);
        order.setSetmealId(Integer.valueOf(map.get("setmealId")));
        //根据预约信息查询t_order表对应订单信息
        List<Order> orderList = orderDao.findByCondition(order);
        //判断是否重复预约：
        if(null != orderList && orderList.size() > 0){
            //存在，重复：抛异常
            throw new SunnydayException("不能重复预约");
        }
        //补充订单信息
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //不存在，没重复,添加订单
        orderDao.add(order);
        //更新已预约数量 t_ordersetting
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return order;
    }
    //回显
    @Override
    public Map<String, Object> findById4Detail(Integer id) {
        return orderDao.findById4Detail(id);
    }


}
